package com.gouaixin.bee.beeframework2.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.google.gson.Gson;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.bean.USER;
import com.gouaixin.bee.beeframework2.tool.mLog;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.GetListener;
import cn.bmob.v3.listener.UpdateListener;


/**
 * Created by Administrator on 2015/3/9.
 */

//征友条件
public class B9_zhengyouActivity extends Activity {
    Context context = B9_zhengyouActivity.this;

    //顶部
    ImageView back;//返回
    TextView title;//标题

    //内容区域
    LinearLayout ageBt;//年龄
    LinearLayout addressBt;//居住地
    LinearLayout hightBt;//身高
    LinearLayout xueliBt;//学历
    LinearLayout shouruBt;//月收入

    TextView ageTv;
    TextView addressTv;
    TextView hightTv;
    TextView xueliTv;
    TextView shouruTv;

    TextView queding;//确定

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.b9_zhengyou);

        initIntent();

        initHead();

        initContent();

        getUserData();


    }

    /***
     *
     */
    private void getUserData() {
        BmobQuery<USER> query = new BmobQuery<USER>();
        query.getObject(context, BmobUser.getCurrentUser(context).getObjectId(), new GetListener<USER>() {
            @Override
            public void onSuccess(USER user) {
                setUserData(user);
            }

            @Override
            public void onFailure(int i, String s) {

            }
        });


    }

    private void setUserData(USER user) {

        mLog.e("设置个人信息:" + new Gson().toJson(user));

        ageTv.setText(user.getAgeft());
        addressTv.setText(user.getAddressft());
        hightTv.setText(user.getHightft());
        xueliTv.setText(user.getXuelift());
        shouruTv.setText(user.getShouruft());

    }

    /**
     * 判断传入的Intent对象
     */
    private void initIntent() {

    }

    /**
     * 设置日期选择器
     */

    private void initDateDialog() {


    }


    /**
     * 内容区域的监听
     */
    private void initContent() {
        ageBt = (LinearLayout) findViewById(R.id.ageBt);
        addressBt = (LinearLayout) findViewById(R.id.addressBt);
        hightBt = (LinearLayout) findViewById(R.id.hightBt);
        xueliBt = (LinearLayout) findViewById(R.id.xueliBt);
        shouruBt = (LinearLayout) findViewById(R.id.shouruBt);

        ageTv = (TextView) findViewById(R.id.ageTv);
        addressTv = (TextView) findViewById(R.id.addressTv);
        hightTv = (TextView) findViewById(R.id.hightTv);
        xueliTv = (TextView) findViewById(R.id.xueliTv);
        shouruTv = (TextView) findViewById(R.id.shouruTv);

        queding = (TextView) findViewById(R.id.queding);


        ageBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent it = new Intent(B9_zhengyouActivity.this, dialogActivity_nianlin.class);
                it.putExtra("flag", "1");
                startActivityForResult(it, 1);


            }
        });

        addressBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent it = new Intent(B9_zhengyouActivity.this, dialogActivity.class);
                it.putExtra("flag", "2");
                startActivityForResult(it, 2);
            }
        });

        hightBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent it = new Intent(B9_zhengyouActivity.this, dialogActivity.class);
                it.putExtra("flag", "3");
                startActivityForResult(it, 3);
            }
        });

        xueliBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent it = new Intent(B9_zhengyouActivity.this, dialogActivity_nianlin.class);
                it.putExtra("flag", "2");
                startActivityForResult(it, 4);
            }
        });

        shouruBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(B9_zhengyouActivity.this, dialogActivity_nianlin.class);
                it.putExtra("flag", "3");
                startActivityForResult(it, 5);
            }
        });

        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }


    /**
     * 头部的监听
     */
    private void initHead() {
        back = (ImageView) findViewById(R.id.back);
        title = (TextView) findViewById(R.id.title);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        if (data != null) {
            android.util.Log.e("Demo", "准备接受返回的数据……" + data.toString());
            String str = data.getStringExtra("str");

            if (requestCode == 1) {
                ageTv.setText(str);
                mLog.e("修改用户信息：开始实现");
                USER myuser = BmobUser.getCurrentUser(context, USER.class);
                myuser.setAgeft(str);
                myuser.update(context, new UpdateListener() {
                    @Override
                    public void onSuccess() {
                        AbToastUtil.showToast(context, "更新用户信息成功");
                        AbDialogUtil.removeDialog(context);
                    }

                    @Override
                    public void onFailure(int i, String s) {
                        AbToastUtil.showToast(context, "更新用户信息失败：" + s);
                        AbDialogUtil.removeDialog(context);

                    }
                });


            }
            if (requestCode == 2) {
                addressTv.setText(str);
                mLog.e("修改用户信息：开始实现");
                USER myuser = BmobUser.getCurrentUser(context, USER.class);
                myuser.setAddressft(str);
                myuser.update(context, new UpdateListener() {
                    @Override
                    public void onSuccess() {
                        AbToastUtil.showToast(context, "更新用户信息成功");
                        AbDialogUtil.removeDialog(context);
                    }

                    @Override
                    public void onFailure(int i, String s) {
                        AbToastUtil.showToast(context, "更新用户信息失败：" + s);
                        AbDialogUtil.removeDialog(context);

                    }
                });
            }

            if (requestCode == 3) {
                hightTv.setText(str);
                mLog.e("修改用户信息：开始实现");
                USER myuser = BmobUser.getCurrentUser(context, USER.class);
                myuser.setHightft(str);
                myuser.update(context, new UpdateListener() {
                    @Override
                    public void onSuccess() {
                        AbToastUtil.showToast(context, "更新用户信息成功");
                        AbDialogUtil.removeDialog(context);
                    }

                    @Override
                    public void onFailure(int i, String s) {
                        AbToastUtil.showToast(context, "更新用户信息失败：" + s);
                        AbDialogUtil.removeDialog(context);

                    }
                });
            }

            if (requestCode == 4) {
                xueliTv.setText(str);
                mLog.e("修改用户信息：开始实现");
                USER myuser = BmobUser.getCurrentUser(context, USER.class);
                myuser.setXuelift(str);
                myuser.update(context, new UpdateListener() {
                    @Override
                    public void onSuccess() {
                        AbToastUtil.showToast(context, "更新用户信息成功");
                        AbDialogUtil.removeDialog(context);
                    }

                    @Override
                    public void onFailure(int i, String s) {
                        AbToastUtil.showToast(context, "更新用户信息失败：" + s);
                        AbDialogUtil.removeDialog(context);

                    }
                });
            }

            if (requestCode == 5) {
                shouruTv.setText(str);
                mLog.e("修改用户信息：开始实现");
                USER myuser = BmobUser.getCurrentUser(context, USER.class);
                myuser.setShouruft(str);
                myuser.update(context, new UpdateListener() {
                    @Override
                    public void onSuccess() {
                        AbToastUtil.showToast(context, "更新用户信息成功");
                        AbDialogUtil.removeDialog(context);
                    }

                    @Override
                    public void onFailure(int i, String s) {
                        AbToastUtil.showToast(context, "更新用户信息失败：" + s);
                        AbDialogUtil.removeDialog(context);

                    }
                });
            }


        }


    }
}
